/**
 *  todolist - 案例
 * @type {module.Application|*}
 */
const Koa = require('koa');
const fs = require('fs');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const StaticCache = require('koa-static-cache');


const app = new Koa();
const router = new Router();


// let datas = {
//     list: [
//         {id:1,title:'<i>事情1</i>',done:true},
//         {id:2,title:'事情2',done:false},
//         {id:3,title:'事情3',done:false}
//     ]
// }

let datas = JSON.parse(fs.readFileSync('./static/data.json'));


/**
 *  处理静态资源
 */
app.use(
    StaticCache(__dirname + '/static',{
        prefix: '/static',  // 需要加的前缀
        gzip: true  // 是否开启gzip压缩
    })
)

app.use(
    StaticCache(__dirname + '/views',{
        prefix: '/views',  // 需要加的前缀
        gzip: true  // 是否开启gzip压缩
    })
)


/**
 * 处理请求正文数据
 */
app.use(bodyParser())


/**
 *  首页
 */
router.get('/', async (ctx, next) => {
    ctx.body = '111';
});

/**
 *  接口todos 数据
 */
router.get('/todos', (ctx, next) => {
    ctx.body = {
        code:0,
        data : datas.list
    }
});


/**
 *  增加数据
 */
router.post('/add',async (ctx, next) => {
    let title = ctx.request.body.title || '';

    if(!title){
        ctx.body = {
            code: 10001,
            data : null,
            msg: '添加的内容不能为空'
        }
    }else{
        let data = {
            id: datas.list.length + 1,
            title:title,
            done:false
        };

        // 改变原来的数据
        datas.list.push(data);

        ctx.body = {
            code: 0,
            data: data,
            msg: 'ok'
        }

        // 写入到json文件内
        fs.writeFileSync('./static/data.json', JSON.stringify(datas))
    }
});


/**
 *  改变状态
 */
router.post('/change',async (ctx, next) => {
    let id = ctx.request.body.id || 0;

    console.log('/change id', id)

    if(!id){
        ctx.body = {
            code: 10001,
            data : null,
            msg: '参数id错误'
        }
    }else{
        // let item = datas.list.filter(data => {
        //     if(data.id == id){
        //         data.done = !data.done;
        //
        //         return data;
        //     }
        // })

        let item = datas.list.find( data=> data.id == id)
        item.done = !item.done;

        console.log('datas.list', datas.list)

        ctx.body = {
            code: 0,
            data: item.done,
            msg: 'ok'
        }

        // 写入到json文件内
        fs.writeFileSync('./static/data.json', JSON.stringify(datas))
    }
});


/**
 *  删除数据
 */
router.post('/remove',async (ctx, next) => {
    let id = ctx.request.body.id || 0;

    console.log('/change id', id)

    if(!id){
        ctx.body = {
            code: 10001,
            data : null,
            msg: '参数id错误'
        }
    }else{

        let newDatas = datas.list.filter( data=> data.id != id)

        datas.list = newDatas;

        ctx.body = {
            code: 0,
            msg: 'ok'
        }

        // 写入到json文件内
        fs.writeFileSync('./static/data.json', JSON.stringify(datas))
    }
});




// 监听错误
router.get('/*', (ctx, next) => {
    // console.log(ctx.response)
    if(ctx.response.status === 404){
        ctx.body = '404, 页面丢失了';
    }
    // ctx.body = ctx.params.id;
});


// 挂在路由
app.use(router.routes());
app.use(router.allowedMethods());

app.listen('3000')